import pandas as pd
import os


current_directory = os.getcwd()

# Navigate up one level
parent_directory = os.path.dirname(current_directory)
# Final path
path = os.path.join(parent_directory, "Data", "SyntheticControl")
wars = ["CIV", "WWI"]
chars = ['adjustability', 'cost', 'materials', 'simplicity', 'production', 'user', 'appliances',\
'durability', 'comfort', 'appearance']

path2 = os.path.join(parent_directory, "Data") + "/"
names = pd.read_csv(\
path2 + "with_header_subclass_all.csv"\
)[['class', "class_title"]].drop_duplicates()
for war in wars:
	for char in chars:
		try:
			weights = pd.read_stata(path + war + char + ".dta")
			weights = weights[weights['_W_Weight'] > 0][['_Co_Number', '_W_Weight']].round(2)
			weights.columns = ['class', "Weight"]
			weights = pd.merge(weights, names, on='class', how='left')
			weights['class_title'] = weights['class_title'].str.title()
			weights['class'] = weights['class'].astype(int)
			weights = weights.sort_values(by = 'Weight', ascending = False)

			export_path = os.path.join(parent_directory, "Tables", "Trait_Donor_Classes_Weights") + "/"

			weights[['class_title', 'class', 'Weight']].to_csv(export_path + war + "_" + char + "_latest.csv", index=False)
		except:
			print("no {} for {}".format(char, war))
        